﻿@page "/project/edit/{Id}"
@page "/project/edit"

@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IPopupService PopupService
@inject IPageModelCatche<ProjectViewModel> _pageModelCatche

<CnGalWebSite.ProjectSite.Shared.Components.Shared.TitleCard Title="@_name" />
<AuthorizeView>
    <Authorized>
        <div>
            @if (Model == null)
            {
                <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
            }
            else
            {
                <CnGalWebSite.Components.Containers.MasaTitleContainer Title="@_name" Icon="@IconType.Project.ToIconString()" Outline>
                 <CnGalWebSite.ProjectSite.Shared.Components.Projects.EditCards.MainCard Model="Model" />
                 <CnGalWebSite.ProjectSite.Shared.Components.Projects.EditCards.ImageCard Model="Model" />
                 <CnGalWebSite.ProjectSite.Shared.Components.Projects.EditCards.PositionGroupCard Model="Model" />
                 <CnGalWebSite.ProjectSite.Shared.Components.Shared.EditCards.SubmitCard Model="Model" OnSubmit="OnSubmit" />
             </CnGalWebSite.Components.Containers.MasaTitleContainer>
            }

        </div>
    </Authorized>
    <NotAuthorized>
        <CnGalWebSite.ProjectSite.Shared.Components.Errors.NotAuthorizedCard />
    </NotAuthorized>
</AuthorizeView>

<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />

@code {
    [Parameter]
    public string Id { get; set; }

    private ProjectEditModel Model { get; set; }
    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;

    string _name = "创建企划";

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override void OnInitialized()
    {
        _name = string.IsNullOrWhiteSpace(Id) ? "创建企划" : "编辑企划";
    }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                Model = await _httpService.GetAsync<ProjectEditModel>($"api/project/edit?id={Id??"0"}");
                StateHasChanged();
            }
            catch (Exception ex)
            {
                //await ErrorHandler.ProcessError(ex, "获取企划信息失败");
            }
        }
    }

    private async Task OnSubmit()
    {
        try
        {
            Model.Identification = await deviceIdentification.GetDeviceIdentificationModel();
            var obj = await _httpService.PostAsync<ProjectEditModel, Result>("api/project/edit", Model);
            //判断结果
            if (obj.Success == false)
            {
                await PopupService.ToastErrorAsync($"{_name}失败", obj.Message);
            }
            else
            {
                await PopupService.ToastSuccessAsync($"{_name}成功");
                _pageModelCatche.Clean($"api/project/get?id={Id}");
                NavigationManager.NavigateTo("/project/" + obj.Message);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, $"{_name}失败");
        }
    }
}
